package com.core.system.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pojo.entity.SystemRolePermissionEntity;
import com.utility.constant.Constant;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 角色权限表  服务类
 * </p>
 *
 * @author hewei
 * @since 2020-11-26
 */
public interface ISystemRolePermissionService extends IService<SystemRolePermissionEntity> {

    /**
     *  根据 roleId 查询关联的数据权限
     * @param roleId 角色Id
     * @return
     */
    default List<SystemRolePermissionEntity> getPermissionList(Long roleId){
            LambdaQueryWrapper<SystemRolePermissionEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SystemRolePermissionEntity::getIsDelete, Constant.ZERO);
            if (!Objects.isNull(roleId)){
                queryWrapper.eq(SystemRolePermissionEntity::getRoleId, roleId);
            }
            return this.getBaseMapper().selectList(queryWrapper);
        }


    /**
     *  删除数据
     * @param entity
     * @return
     */
    default int deleteById(SystemRolePermissionEntity entity){
        LambdaQueryWrapper<SystemRolePermissionEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(entity.getRoleId())){
            lambdaQueryWrapper.eq(SystemRolePermissionEntity::getRoleId,entity.getRoleId());
        }
        if (!Objects.isNull(entity.getPermissionId())){
            lambdaQueryWrapper.eq(SystemRolePermissionEntity::getPermissionId,entity.getPermissionId());
        }
        return this.getBaseMapper().delete(lambdaQueryWrapper);

    }


    /**
     *  批量删除数据
     * @param entityList
     * @return
     */
     default int batchDelete(List<SystemRolePermissionEntity> entityList){
         entityList.forEach( entity ->{
              deleteById(entity);
         });
         return 1;
     }

}
